package fragments;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import com.example.zengyan.myphonebook.R;
import static BD.mConfig.*;
/**
 * Created by zengyan on 2016/6/17.
 */
public class ItemFragment extends DialogFragment {
    private Button callLongBtn,callShortBtn,textMessageBtn;
    private String longnum,shortnum;

    public static ItemFragment newInstance(String longnum,String shortnum){

        Bundle bundle=new Bundle();

        bundle.putString(KEY_LONGNUM, longnum);
        bundle.putString(KEY_SHORTNUM, shortnum);


        ItemFragment itemFragment = new ItemFragment();

        itemFragment.setArguments(bundle);

        return itemFragment;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.itemfragment_layout, container);
        Bundle bundle=getArguments();
        longnum=bundle.getString(KEY_LONGNUM);
        shortnum=bundle.getString(KEY_SHORTNUM);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        callLongBtn= (Button) getView().findViewById(R.id.callLongNum_id);
        callShortBtn= (Button) getView().findViewById(R.id.callShortNum_id);
        textMessageBtn= (Button) getView().findViewById(R.id.textMessage_id);

        callLongBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+longnum));
                startActivity(intent);
            }
        });


        callShortBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+shortnum));
                startActivity(intent);
            }
        });

        textMessageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri smsToUri = Uri.parse("smsto:"+longnum);
                Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
                startActivity(intent);

            }
        });




    }
}
